package com.nbcio.modules.estar.customerprice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.nbcio.modules.erp.base.entity.ErpCustomer;
import com.nbcio.modules.erp.base.service.IErpCustomerService;
import com.nbcio.modules.estar.customerprice.entity.Customerprice;
import com.nbcio.modules.estar.customerprice.mapper.CustomerpriceMapper;
import com.nbcio.modules.estar.customerprice.service.ICustomerpriceService;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 客户价格
 * @Author: nbacheng
 * @Date:   2024-04-28
 * @Version: V1.0
 */
@Service
public class CustomerpriceServiceImpl extends ServiceImpl<CustomerpriceMapper, Customerprice> implements ICustomerpriceService {
    @Lazy
    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private IErpCustomerService erpCustomerService ;
    @Override
    public BigDecimal getPrice(String customerId, String goodsclass, String goodsid,BigDecimal bigDecimal) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (customerId==null){
            QueryWrapper<ErpCustomer> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("phone", sysUser.getPhone());
            ErpCustomer exist = erpCustomerService.getOne(queryWrapper);
            if (exist ==null ){
                return null;
            }
            customerId= exist.getId();
        }
        QueryWrapper<Customerprice> query = new QueryWrapper<>();
        StringBuffer key = new StringBuffer();
        key.append(customerId);
        if (customerId == null){
            return null;
        }
        query.eq("customer", customerId);

        if (goodsid != null&&goodsid.length()>0){
            query.eq("goodsid", goodsid);
            key.append(goodsid);
        }
        java.math.BigDecimal price = (BigDecimal) redisUtil.get(key.toString());
        //如果缓存有数据就直接返回
        if (price!=null){
            return price;
        }
        List<Customerprice> goodslist= this.list(query);
        //如果为空就去找物料分类
        if (!CollectionUtils.isEmpty(goodslist)){
            price= goodslist.get(0).getPrice();
            redisUtil.set(key.toString(), price);
            return price;
        }else {
            key=new StringBuffer();
            key.append(goodsid).append(goodsclass);
            BigDecimal zk= (BigDecimal) redisUtil.get(key.toString());
            //找缓存的数据
            if (zk!=null){
                price= zk.multiply(bigDecimal).divide(new BigDecimal(10));
                return price;
            }
            query = new QueryWrapper<>();
            query.eq("customer", customerId);
            query.eq("goodsclass", goodsclass);
            //物料分类只能找折扣
            goodslist=this.list(query);

            if (!CollectionUtils.isEmpty(goodslist)){
                redisUtil.set(key.toString(), goodslist.get(0).getDiscount());
                price= goodslist.get(0).getDiscount().multiply(bigDecimal).divide(new BigDecimal(10));

            }
        }

        return null;
    }


    public void deleteCache(String customerId, String goodsclass, String goodsid) {
        //删除时删除缓存
        StringBuffer key = new StringBuffer();
        key.append(customerId);
        //如果物料有数据就直接删除缓存
        if (goodsid != null&&goodsid.length()>0){
            key.append(goodsid);
            redisUtil.del(key.toString());
            return;
        }
        //物料没数据就通过分类删除缓存
        if (goodsclass != null&&goodsclass.length()>0){
            key.append(goodsclass);
        }
        redisUtil.del(key.toString());
    }

    public void addAccount(String customerId, String goodsclass, String goodsid, BigDecimal price){
        StringBuffer key = new StringBuffer();
        key.append(customerId);

        //物料有数据直接添加
        if (goodsid != null&&goodsid.length()>0){
            key.append(goodsid);
            redisUtil.set(key.toString(), price);
            return;
        }
        //物料没数据就通过分类添加
        if (goodsclass != null&&goodsclass.length()>0){
            key.append(goodsclass);
            redisUtil.set(key.toString(), price);
        }


    }
    public boolean save(Customerprice entity){
        //物料有数据直接添加
        if (entity.getGoodsid()!=null&&entity.getGoodsclass().length()>0){
            this.addAccount(entity.getCustomer(), entity.getGoodsclass(), entity.getGoodsid(), entity.getPrice());
        }else {
        //物料没数据就通过分类添加，分类添加的未折扣
            this.addAccount(entity.getCustomer(), entity.getGoodsclass(), entity.getGoodsid(), entity.getDiscount());
        }

        return super.save(entity);
    }

    public boolean updateById(Customerprice entity){
        //物料有数据直接添加
        if (entity.getGoodsid()!=null&&entity.getGoodsclass().length()>0){
            this.addAccount(entity.getCustomer(), entity.getGoodsclass(), entity.getGoodsid(), entity.getPrice());
        }else {
            //物料没数据就通过分类添加，分类添加的未折扣
            this.addAccount(entity.getCustomer(), entity.getGoodsclass(), entity.getGoodsid(), entity.getDiscount());
        }
        return super.updateById(entity);
    }

    public boolean removeById(Serializable id){
        Customerprice customerprice = this.getById(id);
        this.deleteCache(customerprice.getCustomer(), customerprice.getGoodsclass(), customerprice.getGoodsid());
        return super.removeById(id);
    }
    public boolean removeByIds(Collection<?> list) {
        for (Object id : list){
            Customerprice customerprice = this.getById((String)id);
            this.deleteCache(customerprice.getCustomer(), customerprice.getGoodsclass(), customerprice.getGoodsid());
        }
        return super.removeByIds(list);
    }
}
